<?php
error_reporting(0);

include("inc_resize2.php");
	/*
		* 此页面做两个操作
		* 1 对第一张图进行缩略 命名：1212121_icon.jpg
		* 2 然后对所有图片添加水印，（顺序不能颠倒，这样能保证首页的图片没有水印）
	
	*/
	

	// ------------------------------------------------------------------------
	/**
		* 将图片字符串变成数组
	*/
	function getarrp($pic)
	{
		$arrurl =  preg_split("/@/",$pic);
		return $arrurl;
	}

	
	// ------------------------------------------------------------------------
	/*
		* 格式： a.jpg@b.jpg@c.jpg@
	
	*/
	if(isset($_GET['imgurls']))
	{
		$imgurls =  $_GET['imgurls'];
	}
	else
	{
		$imgurls = "a.jpg@b.jpg@c.jpg@";
	}
	
	//图片组
	$pic_group = trim($_GET['pg']);
	if (!in_array($pic_group, array('vUser', 'shop'))) {
		$pic_group = 'vUser';
	}
	
	$arrpic = getarrp($imgurls);
	$att = "_icon";
	$filepath_old = "../../jiaju369img/user/";		//老的文件路径
	$filepath = "../../jiaju369img/{$pic_group}/"; // 上传后的路径

	for($i = 0;$i+1 < count($arrpic);$i++):
		// 对第一张图，做缩略图
		if($i == 0)
		{
			if (file_exists($filepath.$arrpic[$i])) {//是否是新的目录结构的情况
				if (! check_icon($arrpic, $pic_group)) {
					set_picpath($arrpic[$i], $pic_group);
				}
				new resizeimage($filepath.$arrpic[$i],"231","220",$att);
			}else {//老目录结构
				check_icon($arrpic, $pic_group);
				new resizeimage($filepath_old.$arrpic[$i],"231","220",$att);
			}
		}
		// 然后对所有图做水印
		//setWater($filepath.$arrpic[$i],"logo.png","WWW.JiaJu369.Com","254,238,039","7","arial.ttf","img");
	endfor;
	echo 1;
	
	// -------------------------------------------------------------------------
	/**
	 * 设置、更新文件路径信息
	 *
	 * @see		set_picpath()
	 * @author	Laurence.xu <haohailuo@163.com>
	 * @version	Sun Mar 13 09:45:53 CST 2011
	*/
	function set_picpath($img, $pic_group) {
		$path_info_file = "./path_info_{$pic_group}.data";
		
		$path_1 = $path_2 = $path_3 = '000';
		$count = 0;
		if (file_exists($path_info_file)) {
			$path_info = file($path_info_file);
			$path_info = array_map('trim', $path_info);
			list($path_1, $path_2, $path_3, $count) = $path_info;
		}
		
		//基本路径
		$basepath = "../../jiaju369img/{$pic_group}/";
		
		//路径信息
		list($path_1_new, $path_2_new, $path_3_new, $img_name) = explode('/', $img);
		
		if ($path_1_new == $path_1 && $path_2_new == $path_2 && $path_3_new == $path_3) {
			$count++;
			
			$path_info_str = "{$path_1}\n{$path_2}\n{$path_3}\n{$count}";
			file_put_contents($path_info_file, $path_info_str);
		}
		
		return true;
	}
	
	/**
	 * 判断是否存在缩略图
	 *
	 * @see		check_icon()
	 * @author	Laurence.xu <haohailuo@163.com>
	 * @version	Sun Mar 13 13:08:07 CST 2011
	 * @return	<rType> $rValueName rInfo
	*/
	function check_icon($picarr = array(), $pic_group) {
		$basepath = "../../jiaju369img/{$pic_group}/";
		$basepath_old = "../../jiaju369img/user/";
		
		//兼容老结构
		if (file_exists($basepath_old.$picarr[0])) {
			$basepath = $basepath_old;
		}
		
		$has_icon = false;
		foreach ($picarr as $k=>$val) {
			if (empty($val)) continue;
			list($name, $ext) = explode('.', $val);
			$icon = $basepath.$name.'_icon.'.$ext;
			
			if (file_exists($icon)) {
				$has_icon = true;
				if ($k != 0) {
					@unlink($icon);
				}
			}
		}
		
		return $has_icon;
	}